﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.product
{
    public partial class productparameter_info_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string classValue = ChangeHope.WebPage.PageRequest.GetString("classValue");
                if (classValue != "")
                {
                    ViewState["ClassValue"] = classValue;
                }
                int parameterGroupId = ChangeHope.WebPage.PageRequest.GetInt("ParGid");
                int propertyTypeId = ChangeHope.WebPage.PageRequest.GetInt("PTId");
                if (parameterGroupId > 0)
                {
                    ViewState["ParameterGroupId"] = parameterGroupId;
                }
                if (propertyTypeId > 0)
                {
                    ViewState["PropertyTypeId"] = propertyTypeId;
                }
                int parameterId = ChangeHope.WebPage.PageRequest.GetInt("id");
                if (parameterId > 0 && propertyTypeId > 0)
                {
                    BindInfo(parameterId, propertyTypeId);
                }
                this.HyperLink2.NavigateUrl = "productparameter_info_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + ViewState["ParameterGroupId"] + "&PTId=" + ViewState["PropertyTypeId"] + "";
                InitWebControl();
            }
        }

        protected void BindInfo(int id, int productTypeId)
        {
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            YXShop.Model.Product.ProductTypeParameter model = bll.GetProductParameterModel(id,productTypeId);
            if (model != null)
            {
                this.txtName.Text = model.ParameterName;
                this.txtDataValue.Text = model.ParameterValue;
                this.txtSort.Text = model.Sort.ToString();
                this.ddlType.SelectedValue = model.ParameterType.ToString();
                if (model.ParameterType.ToString() == "4" || model.ParameterType.ToString() == "5")
                {
                    this.trDatavalue.Style.Value = "display:none";
                }
                ViewState["ID"] = model.ID;
            }
        }
        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "输入商品的参数名称,您再添加商品时会自动加载该参数.", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtSort, "输入数字将作为显示的排列顺序", "isint", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            YXShop.Model.Product.ProductTypeParameter model = new YXShop.Model.Product.ProductTypeParameter();
            model.ParameterName = this.txtName.Text;
            model.ParameterType = Convert.ToInt32(this.ddlType.SelectedValue);
            model.ParameterValue = this.txtDataValue.Text;
            model.ParID = Convert.ToInt32(ViewState["ParameterGroupId"]);
            model.Sort = Convert.ToInt32(this.txtSort.Text);
            model.TypeID = Convert.ToInt32(ViewState["PropertyTypeId"]);
            if (ViewState["ID"] == null)
            {
                bll.AddProductParameter(model);
                ChangeHope.WebPage.BasePage.PageRight("添加成功，您还可以继续添加。", "productparameter_info_edit.aspx?classValue=" + ViewState["ClassValue"] + "&PTId=" + ViewState["PropertyTypeId"] + "&ParGid=" + ViewState["ParameterGroupId"]);
            }
            else
            {
                model.ID = Convert.ToInt32(ViewState["ID"]);
                if (bll.UpdateProductParameter(model,Convert.ToInt32(ViewState["PropertyTypeId"])) > 0)
                {
                    ChangeHope.WebPage.BasePage.PageRight("修改成功。", "productparameter_info_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + ViewState["ParameterGroupId"] + "&PTId=" + ViewState["PropertyTypeId"] + "");
                }
            }
        }
    }
}
